<script lang="ts" setup name="CallbackPatch">
import { ref } from 'vue';
import Message from '@/components/message'
import useStore from '@/store'
import {useRouter} from 'vue-router';
const router = useRouter()
const {user} = useStore()

// 获取openId
const isLogin = QC.Login.check()
let openId = ''
if(isLogin){
    QC.Login.getMe(function(id){
          openId = id
    })
}

const form = ref({
    account:'',
    mobile:'',
    code:'',
    password:''
})

// 获取验证码
let time = ref(0)
const send = async ()=>{
    if(time.value > 0) return
    await user.sendQQPathMsg(form.value.mobile)
    Message.success('获取验证码成功')

    // 倒计时
    time.value = 60
    let timeId = -1
    timeId = window.setInterval(()=>{
        time.value--
        if(time.value===0){
            clearInterval(timeId)
        }
    },1000)
}

// 提交信息
const submit = async () => {
    await user.qqPatchLogin({
        ...form.value,
        openId
    })
    Message.success('绑定用户信息成功')
    router.push('/')
}


</script>
<template>
  <div class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input v-model="form.account" class="input" type="text" placeholder="请输入用户名" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input v-model="form.mobile" class="input" type="text" placeholder="请输入手机号" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input v-model="form.code" class="input" type="text" placeholder="请输入验证码" />
        <span class="code" @click="send"> 
            {{time===0?'发送验证码':`${time}s后发送`}}
        </span>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input v-model="form.password" class="input" type="password" placeholder="请输入密码" />
      </div>
      <div class="error"></div>
    </div>
    
    <a href="javascript:;" class="submit" @click="submit">立即提交</a>
  </div>
</template>

<style scoped lang="less">
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
